<?php

namespace App\Services\Admin;



use App\Http\Common\Constant;
use App\Http\Models\Admin\CMS\Post;
use App\Http\Models\Admin\File\File;
use App\Http\Models\Admin\File\FileContent;
use App\Http\Models\Admin\Sys\SysParam;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\DB;

class CommonService
{

    public function getSiteMapList(){
        $static_list = FileContent::select('id','title','name')->where('parent_id','0')->orderBy('show_index','asc')->get();

        $find = SysParam::where('name',Config::get('common.system_sitemap_tag',"system_sitemap_tag"))->first();


        $frens = Config::get('common.sitemap_frequency');
        $pros = Config::get('common.sitemap_priorities');

        if($find){
            $value = unserialize($find->value);
            $collection = collect($value);
            $keyed = $collection->keyBy('name');
            $result = $keyed->all();

            foreach($static_list as &$item){

                if(array_has($result,$item->name)){
                    $pro =  $result[$item->name];

                        $item->frenquency  = $pro['frenquency'];
                        $item->priorite = $pro['priorite'];
                }else{
                    if($frens){
                        $item->frenquency =$frens[0];
                    }else{
                        $item->frenquency ='Always';
                    }
                    if($pros){
                        $item->priorite =$pros[0];
                    }else{
                        $item->priorite = 0.0;
                    }

                }

                $item->modified  = date("Y-m-d")."T".date("H:i:s")."+00:00";

            }
        }
        return $static_list;

    }


    /**
     * 获取网站的信息
     */
    public  function getSiteMsg(){
        $seos = SysParam::where('type',Config::get('common.system_type_seo'))->get();
        $seo = array();
        if($seos){
            foreach($seos as $item){
                $seo[$item->name] = $item->value;
            }
        }

        return $seo;
    }
    public  function getLeastPost($limit=20){
        $posts =  Post::select('id','title','created_at','load_img','post_content')->where('post_status','1')->orderBy('created_at','desc')->take($limit)->get();
        if($posts){
            foreach($posts as $item){
                if($item->load_img){
                    if(Constant::needUrl($item->load_img)){
                        $item->load_img = url($item->load_img);
                    }

                    $item->desc = '<img src="'.$item->load_img.'" width="400" /></br>';
                }
                $item->post_content = $item->desc.Constant::subStr($item->post_content);
                $item->href =url('blog/'.$item->id);
            }
        }
        return $posts;
    }
}
